package com.fhz.kotlinmvvm.main

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.fhz.kotlinmvvm.MainActivity
import com.fhz.kotlinmvvm.R
import com.fhz.kotlinmvvm.databinding.GoodsItemLayoutBinding
import com.fhz.kotlinmvvm.entity.Goods

/**
 * 时间:2023/9/14
 * @author Mr.Feng
 * 简述:
 */
class GoodsRVAdapter(val contenxt:Context,
                     val list:List<Goods>,
                     val myItemClickListener: (Goods) -> Unit
) : RecyclerView.Adapter<GoodsRVAdapter.ViewHolder>() {

    inner class ViewHolder( binding: GoodsItemLayoutBinding) : RecyclerView.ViewHolder(binding.root){

        //databinding的点击事件.
//        fun bind(goods:Goods){
////            binding.itemGoods = goods
////            binding.myClick = contenxt as MainActivity
//        }


        var item: Goods? = null
        val imageView = binding.goodsItemPic
        val textView  = binding.goodsItemTitle
        init{
            binding.root.setOnClickListener {
                item?.let{
                    myItemClickListener(it)
                }
            }
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflate = LayoutInflater.from(contenxt)
        val inflate1 = DataBindingUtil.inflate<GoodsItemLayoutBinding>(
            inflate,
            R.layout.goods_item_layout,
            parent,
            false
        )
        return ViewHolder(inflate1)
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val goods = list[position]
        //点击
//        holder.bind(goods)
        Glide.with(contenxt).load(goods.goodsDefaultIcon).into(holder.imageView)
        holder.textView.text = goods.goodsDesc
        holder.item = goods
    }

    override fun getItemCount(): Int = list.size
}